package br.com.egg.finances.business.service;

import java.sql.Connection;
import java.sql.SQLException;

import br.com.egg.finances.business.dao.util.BDUtils;

class EntityService {

	protected Connection connection;

	public EntityService() {
		this.connection = BDUtils.getConnection();
	}

	public EntityService(Connection connection) {
		super();
		this.connection = connection;
	}

	public Connection getConnection() throws SQLException {
		if (connection.isClosed()) {
			connection = BDUtils.getConnection();
		}
		return connection;
	}

	public void useConnection(Connection connection) {
		if (this.connection != null) {
			BDUtils.closeResources(connection, null, null);
		}
		this.connection = connection;
	}
	
	public void commitTransaction() {
		try {
			this.connection.commit();
		} catch (Exception e) {
			System.err.println("ERRO -- COMMIT");
			e.printStackTrace();
		}
	}
	
	public void rollbackTransaction() {
		try {
			this.connection.rollback();
		} catch (Exception e) {
			System.err.println("ERRO -- ROLLBACK");
			e.printStackTrace();
		}
	}

}
